#include <string>
#include "socket_module/socket.h"
#include "test_util.h"
#include "loop.h"
#include "./../../log_module/log_module.h"
#include "socket_module/socket_channel.h"
#include <cstdio>
#include <functional>
#include <gtest/gtest.h>
#include <memory>
#include <spdlog/spdlog.h>

void ReadCallback(std::shared_ptr<ChannelProtocol> channel, std::shared_ptr<Description> socket)
{
    SPDLOG_INFO("read callback fucntion");
}

void ErrorCallback(std::shared_ptr<ChannelProtocol> channel, std::shared_ptr<Description> socket)
{
    printf("error callback function.\n");
    channel->DelEvent();
}

void WriteCallback(std::shared_ptr<ChannelProtocol> channel, std::shared_ptr<Description> socket)
{
    printf("write callback function.\n");
    channel->ModEvent(EmIoIn | EmIoErr | EmIoOneShot | EmIoRdHup);
}

TEST(ChannelTest, ChannelClient)
{
    Loop loop; 
    /* Loop loop(EPOLL); */
    int client_socket = CreateClientSocket();
    auto socket_channel = std::make_shared<SocketChannel>(&loop, std::make_shared<SocketDescription>(client_socket));
    printf("new client socket : %d.\n",client_socket);
    socket_channel->AddEvent(EmIoIn | EmIoOut | EmIoErr | EmIoRdHup | EmIoOneShot);

    socket_channel->SetReadFunction(std::bind(ReadCallback, std::placeholders::_1, std::placeholders::_2));
    socket_channel->SetWriteFunction(std::bind(WriteCallback, std::placeholders::_1, std::placeholders::_2));
    socket_channel->SetErrorFunction(std::bind(ErrorCallback, std::placeholders::_1, std::placeholders::_2));
    loop.Run();
}

int main(int argc, char * argvi[])
{
    testing::InitGoogleTest(&argc, argvi);
    LogModuleConfig config;
    config.m_file_name = argvi[0];
    config.m_log_level = "trace";
    LogModule::Instance()->InitModule(config);
    return RUN_ALL_TESTS();
}
